﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class TypeDAO : MarshalByRefObject, IType
    {
        protected DbHelper dbHelper = new DbHelper();


        #region 统计类型一

        //根据编码查询统计类型一
        public MisModel.Type GetType1ById(string TpId)
        {
            if (string.IsNullOrEmpty(TpId))
                return null;
            string sql = string.Format("select TP1_ID,TYPE1,f1 from TYPE1_TAB where TP1_ID='{0}'", TpId);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.Type entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.Type();
                entity.TpId = dt.Rows[0]["TP1_ID"].ToString();
                entity.TypeName = dt.Rows[0]["TYPE1"].ToString();
                entity.F1 = dt.Rows[0]["f1"].ToString();
            }
            return entity;
        }

        //查询统计类型一
        public DataTable GetType1()
        {
            string sql = "select TP1_ID,TYPE1,f1,(TP1_ID + '.' + TYPE1) as ItemShow  from TYPE1_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加统计类型一
        public int AddType1(MisModel.Type obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into TYPE1_TAB( TP1_ID,TYPE1,f1) values('{0}','{1}','{2}')", obj.TpId, obj.TypeName, obj.F1);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改统计类型一
        public int UpdateType1(MisModel.Type obj)
        {
            string sql = string.Format("update TYPE1_TAB set TYPE1='{0}',F1='{1}' where TP1_ID='{2}'", obj.TypeName, obj.F1, obj.TpId);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除统计类型一
        public int DeleteType1(string id)
        {
            string sql = string.Format("delete from TYPE1_TAB where TP1_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }

        #endregion

        #region 统计类型二

        //根据编码查询统计类型二
        public MisModel.Type GetType2ById(string TpId)
        {
            if (string.IsNullOrEmpty(TpId))
                return null;
            string sql = string.Format("select TP2_ID,TYPE2,f1 from TYPE2_TAB where TP2_ID='{0}'", TpId);
            DataTable dt = dbHelper.ExecuteDataTable(sql);
            MisModel.Type entity = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                entity = new MisModel.Type();
                entity.TpId = dt.Rows[0]["TP2_ID"].ToString();
                entity.TypeName = dt.Rows[0]["TYPE2"].ToString();
                entity.F1 = dt.Rows[0]["f1"].ToString();
            }
            return entity;
        }

        //查询统计类型二
        public DataTable GetType2()
        {
            string sql = "select TP2_ID,TYPE2,f1,(TP2_ID + '.' + TYPE2) as ItemShow  from TYPE2_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加统计类型二
        public int AddType2(MisModel.Type obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into TYPE2_TAB( TP2_ID,TYPE2,f1) values('{0}','{1}','{2}')", obj.TpId, obj.TypeName, obj.F1);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改统计类型二
        public int UpdateType2(MisModel.Type obj)
        {
            string sql = string.Format("update TYPE2_TAB set TYPE2='{0}',F1='{1}' where TP2_ID='{2}'", obj.TypeName, obj.F1, obj.TpId);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除统计类型二
        public int DeleteType2(string id)
        {
            string sql = string.Format("delete from TYPE2_TAB where TP2_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }

        #endregion

        #region 统计类型三

        //根据编码查询统计类型三
        public MisModel.Type GetType3ById(string TpId)
        {
            if (string.IsNullOrEmpty(TpId))
                return null;
            string sql = string.Format("select TP3_ID,TYPE3,f1 from TYPE3_TAB where TP3_ID='{0}'", TpId);
            DataTable dt = dbHelper.ExecuteDataTable(sql);
            MisModel.Type entity = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                entity = new MisModel.Type();
                entity.TpId = dt.Rows[0]["TP3_ID"].ToString();
                entity.TypeName = dt.Rows[0]["TYPE3"].ToString();
                entity.F1 = dt.Rows[0]["f1"].ToString();
            }
            return entity;
        }

        //查询统计类型三
        public DataTable GetType3()
        {
            string sql = "select TP3_ID,TYPE3,f1,(TP3_ID + '.' + TYPE3) as ItemShow  from TYPE3_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加统计类型三
        public int AddType3(MisModel.Type obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into TYPE3_TAB( TP3_ID,TYPE3,f1) values('{0}','{1}','{2}')", obj.TpId, obj.TypeName, obj.F1);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改统计类型三
        public int UpdateType3(MisModel.Type obj)
        {
            string sql = string.Format("update TYPE3_TAB set TYPE3='{0}',F1='{1}' where TP3_ID='{2}'", obj.TypeName, obj.F1, obj.TpId);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除统计类型三
        public int DeleteType3(string id)
        {
            string sql = string.Format("delete from TYPE3_TAB where TP3_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }

        #endregion
    }
}
